package gameengine.com.actor;

import resolution.DEF;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.view.KeyEvent;
import gameengine.com.GameLib;
import gameengine.com.MainActivity;
import gameengine.com.Map;
import gameengine.com.Sprite;
import gameengine.com.state.StateGameplay;

public class Explorer extends Actor// Nha tham hiem
{

	public static final int FRAMES = 4;

	public static final int STATE_IDLE = 0;
	public static final int STATE_MOVE = 1;
	public static final int STATE_GETHURT = 2;
	public static final int STATE_DIE = 3;
	public static final int STATE_WIN = 4;

	public static final int ANIM_IDLE_DOWN = 0;
	public static final int ANIM_IDLE_LEFT = 1;
	public static final int ANIM_IDLE_RIGHT = 2;
	public static final int ANIM_IDLE_UP = 3;
	public static final int ANIM_RUN_DOWN = 4;
	public static final int ANIM_RUN_LEFT = 5;
	public static final int ANIM_RUN_RIGHT = 6;
	public static final int ANIM_RUN_UP = 7;

	public static boolean isDie = false;
	public Sprite sprite;

	public Explorer(int row, int col, int w, int h)
	{
		// super(paramImage, 18, 18);
		m_pos_rows = row;
		m_pos_cols = col;
		sprite = new Sprite("sprite/actor/mushroom.sprt"); 

		m_x = col * Map.MAP_CELL_WIDTH + Map.MAP_CELL_WIDTH / 2;
		m_y = row * Map.MAP_CELL_HEIGHT + Map.MAP_CELL_HEIGHT / 2;

		m_Width = w;
		m_Height = h;
		this.mdirection = 0;
		this.mstate = STATE_IDLE;
		isDie = false;
		// this.isFinished = false;
	}

	public void setPos(int x, int y)
	{
		m_pos_rows = y;
		m_pos_cols = x;
		m_x = m_pos_cols * Map.MAP_CELL_WIDTH + Map.MAP_CELL_WIDTH / 2;
		m_y = m_pos_rows * Map.MAP_CELL_HEIGHT + Map.MAP_CELL_HEIGHT / 2;

	}

	public void draw(Canvas c)
	{
		MainActivity.mainPaint.setStyle(Style.STROKE);
		switch (mstate)
		{
		case STATE_IDLE:
			MainActivity.mainPaint.setColor(Color.RED);
			break;
		case STATE_MOVE:
			MainActivity.mainPaint.setColor(Color.BLUE);
			break;
		case STATE_GETHURT:
			MainActivity.mainPaint.setColor(Color.GREEN);
			break;
		case STATE_DIE:
			MainActivity.mainPaint.setColor(Color.YELLOW);
			break;
		case STATE_WIN:
			MainActivity.mainPaint.setColor(Color.WHITE);
			break;
		}
		///	sprite.set
		sprite.drawAnim(c);
		//	c.drawCircle(m_x, m_y, m_Width, MainActivity.mainPaint);
	}

	public void update()
	{
		switch (mstate)
		{
		case STATE_IDLE:
			updateStateIDE();
			break;
		case STATE_MOVE:
			updateStateMove();
			break;
		case STATE_GETHURT:

			break;
		case STATE_DIE:

			break;
		case STATE_WIN:
			updateStateWIN();
			break;
		default:
			break;
		}

	}

	void updateStateIDE()
	{

		if (StateGameplay.isCreatureMoving()) //Dont update IDE while mummy is moving
			return;

		if (isDie) {
			setState(STATE_DIE);
			return;
		}

		if (StateGameplay.map.isStandAtExit(m_pos_cols, m_pos_rows)) //check WIN condition
		{
			setState(STATE_WIN);
			return;
		}

		if (GameLib.isKeyReleased(KeyEvent.KEYCODE_DPAD_UP) || (MainActivity.isTouchReleaseInRect(DEF.DPAD_TOP_X, DEF.DPAD_TOP_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))) {
			if (!StateGameplay.map.checkWallCollition(DIRECTION_UP, m_pos_cols, m_pos_rows)) {
				m_Target_Pos_Rows = m_pos_rows - 1;
				m_Target_Pos_Cols = m_pos_cols;
				mdirection = DIRECTION_UP;
				mstate = STATE_MOVE;
				sprite.setAnim(ANIM_RUN_UP, true, false);
			}
		} else if (GameLib.isKeyReleased(KeyEvent.KEYCODE_DPAD_DOWN) || (MainActivity.isTouchReleaseInRect(DEF.DPAD_BOTTOM_X, DEF.DPAD_BOTTOM_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))) {
			if (!StateGameplay.map.checkWallCollition(DIRECTION_DOWN, m_pos_cols, m_pos_rows)) {
				m_Target_Pos_Rows = m_pos_rows + 1;
				m_Target_Pos_Cols = m_pos_cols;
				mdirection = DIRECTION_DOWN;
				mstate = STATE_MOVE;
				sprite.setAnim(ANIM_RUN_DOWN, true, false);
			}
		} else if (GameLib.isKeyReleased(KeyEvent.KEYCODE_DPAD_LEFT) || (MainActivity.isTouchReleaseInRect(DEF.DPAD_LEFT_X, DEF.DPAD_LEFT_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))) {
			if (!StateGameplay.map.checkWallCollition(DIRECTION_LEFT, m_pos_cols, m_pos_rows)) {
				m_Target_Pos_Rows = m_pos_rows;
				m_Target_Pos_Cols = m_pos_cols - 1;
				mdirection = DIRECTION_LEFT;
				mstate = STATE_MOVE;
				sprite.setAnim(ANIM_RUN_LEFT, true, false);
			}
		} else if (GameLib.isKeyReleased(KeyEvent.KEYCODE_DPAD_RIGHT) || (MainActivity.isTouchReleaseInRect(DEF.DPAD_RIGHT_X, DEF.DPAD_RIGHT_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))) {
			if (!StateGameplay.map.checkWallCollition(DIRECTION_RIGHT, m_pos_cols, m_pos_rows)) {
				m_Target_Pos_Rows = m_pos_rows;
				m_Target_Pos_Cols = m_pos_cols + 1;
				mdirection = DIRECTION_RIGHT;
				mstate = STATE_MOVE;
				sprite.setAnim(ANIM_RUN_RIGHT, true, false);
			}
		} else if (GameLib.isKeyReleased(KeyEvent.KEYCODE_DPAD_CENTER) ||(GameLib.isKeyReleased(KeyEvent. KEYCODE_ENTER))||
				(MainActivity.isTouchReleaseInRect(DEF.DPAD_TOP_X, DEF.DPAD_TOP_Y + DEF.DPAD_HEIGHT, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT))) {
			
				setState(STATE_IDLE);
			
		}
		if (m_Target_Pos_Rows != m_pos_rows || m_Target_Pos_Cols != m_pos_cols) {
			m_Target_x = m_Target_Pos_Cols * Map.MAP_CELL_WIDTH + Map.MAP_CELL_WIDTH / 2;
			m_Target_y = m_Target_Pos_Rows * Map.MAP_CELL_HEIGHT + Map.MAP_CELL_HEIGHT / 2;
		}
	}

	void updateStateMove()
	{
		if ((Math.abs(m_x - m_Target_x) <= m_Speed) && ((m_x - m_Target_x) != 0)) {
			m_x = m_Target_x;

			m_pos_cols = m_Target_Pos_Cols;
			m_pos_rows = m_Target_Pos_Rows;
			if (mdirection == DIRECTION_LEFT)
				sprite.setAnim(ANIM_IDLE_LEFT, m_x, m_y, true, false);
			else
				sprite.setAnim(ANIM_IDLE_RIGHT, m_x, m_y, true, false);
			setState(STATE_IDLE);
			sprite.setPos(m_x, m_y);

			return;// vi chi duy chuyen theo doc hoac ngang
		}
		if ((Math.abs(m_y - m_Target_y) <= m_Speed) && ((m_y - m_Target_y) != 0)) {
			m_y = m_Target_y;
			m_pos_cols = m_Target_Pos_Cols;
			m_pos_rows = m_Target_Pos_Rows;
			if (mdirection == DIRECTION_DOWN)
				sprite.setAnim(ANIM_IDLE_DOWN, m_x, m_y, true, false);
			else
				sprite.setAnim(ANIM_IDLE_UP, m_x, m_y, true, false);
			setState(STATE_IDLE);
			sprite.setPos(m_x, m_y);
			return;// vi chi duy chuyen theo doc hoac ngang
		}
		switch (mdirection)
		{
		case DIRECTION_UP:
			m_y -= m_Speed;
			break;
		case DIRECTION_DOWN:
			m_y += m_Speed;
			break;
		case DIRECTION_LEFT:
			m_x -= m_Speed;
			break;
		case DIRECTION_RIGHT:
			m_x += m_Speed;
			break;
		default:
			break;
		}
		sprite.setPos(m_x, m_y);
	}

	void updateStateWIN()
	{

	}

	void setState(int state)
	{
		if ((mstate == STATE_MOVE||mstate == STATE_IDLE)&& state == STATE_IDLE) {
			for (int i = 0; i < StateGameplay.creature.size(); i++)
				StateGameplay.creature.get(i).setstate(Creature.STATE_MOVE);
		}
		mstate = state;
	}
}

/*
 * Location: C:\Users\toanstt\Desktop\mummymaze_xbd3dnff.jar Qualified Name:
 * Explorer JD-Core Version: 0.6.0
 */